package com.zhangye.test.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhangye.test.entity.EasyArResult;
import com.zhangye.test.mapper.AdminMapper;
import com.zhangye.test.mapper.EasyArMapper;
import com.zhangye.test.mapper.ProxyArMapper;
import com.zhangye.test.mapper.ScanCountMapper;
import com.zhangye.test.model.Admin;
import com.zhangye.test.model.EasyAr;
import com.zhangye.test.model.ProxyAr;
import com.zhangye.test.model.ProxyScanCount;
import com.zhangye.test.model.ScanCount;
import com.zhangye.test.util.cloudUtils;

/**
 * 扫描次数
 * 作者： 张叶
 * 创建时间：2018年6月14日  上午10:56:53
 */
@Service
public class ScanCountService {

	@Autowired
	private ScanCountMapper scanCountMapper;
	
	@Autowired
	private EasyArMapper easyArMapper;
	
	@Autowired
	private AdminMapper adminMapper;
	
	@Autowired
	private SendMsgService sendMsgService;
	
	@Autowired
	private ProxyArMapper proxyArMapper;
	
	@Autowired
	private ProxyScanCountService proxyScanCountService;
	
	
	/**
	 * 查询所有
	 * 作者： 张叶
	 * 创建时间：2018年6月14日  上午10:58:02
	 */
	public List<ScanCount> selectAll(){
		List<ScanCount> list = scanCountMapper.selectAll();
		return list;
	}
	public List<ScanCount> selectByWhere(String where){
		List<ScanCount> list = scanCountMapper.selectByWhere(where);
		return list;
	}
	
	
	/**
	 * 添加
	 * 视频如果为客户上传的需要：
	 * 1，admin对应免费次数counts字段－1
	 * 2，如果count次数为0时，所有视频修改此视频为禁用状态，短信提示充值
	 * 
	 * 作者： 张叶
	 * 创建时间：2018年6月14日  上午10:58:23
	 */
	@Transactional
	public int insert1(ScanCount sc){
		int i = 0;
		String where = " and video_url = '" + sc.getVideoUrl() + "'";
		List<EasyAr> list = easyArMapper.selectByWhere(where);
		if(list.size() > 0){//制作视频
			if(list.get(0).getAdminId() != null){//确保有管理员
				Admin admin = adminMapper.selectByPrimaryKey(list.get(0).getAdminId());
				if(admin != null && !"".equals(admin)){
					int count = admin.getCounts() - 1;
					if(count < 0){//没有剩余扫描次数
						updateEasyArStatus(admin);
					}else{
						admin.setCounts(count);
						i += adminMapper.updateByPrimaryKey(admin);
						sc.setImageUrl(list.get(0).getImageUrl());	
						i += scanCountMapper.insert(sc);
					}
				}else{
					i += scanCountMapper.insert(sc);
				}
			}else{
				i += scanCountMapper.insert(sc);	
			}
		}else{
			i += scanCountMapper.insert(sc);
			//查找是否为代理
			List<ProxyAr> listPa = proxyArMapper.selectByWhere(" and video_url = '" + sc.getVideoUrl() + "'");
			if(listPa.size() > 0){
				ProxyScanCount psc = new ProxyScanCount();
				psc.setVideoUrl(sc.getVideoUrl());
				psc.setImageUrl(listPa.get(0).getImageUrl());
				psc.setAddress(sc.getAddress());
				psc.setPhoneType(sc.getPhoneType());
				psc.setCreatedAt(new Date());
				proxyScanCountService.insert(psc);
			}
		}
		return i;
	}
	
	
	private void updateEasyArStatus(Admin admin) {
		List<EasyAr> list;
		String where = " and admin_id = " + admin.getId();
		list = easyArMapper.selectByWhere(where);
		for(EasyAr ea : list){//修改easyAr的状态
			if(ea.getActive() != 0){
				ea.setActive(0);
				EasyArResult er;
				try {
					er = cloudUtils.updateCloud(ea.getTargetId(),ea.getActive().toString(),ea.getName());
					String status = er.getStatusCode();
					if(status.equals("0")){//修改成功，改库
						easyArMapper.updateByPrimaryKey(ea);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		//短信提醒
		try {
//			smsService.sendNotice(admin.getTelphone());
			sendMsgService.sendMsg(admin.getTelphone(), 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public int insert(ScanCount sc){
		return scanCountMapper.insert(sc);
	}
	
	
	public List<Map<String,Object>> selectByCounts(String where){
		List<Map<String,Object>> list = scanCountMapper.selectByCounts(where);
		return list;
	}
	
}
